<template>
  <div></div>
</template>

<script>
import { auth } from 'api/home-api'
import { getUrlParams } from '../../static/js/utils.js'
export default {
  data () {
    return {
      user: {}
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    getCode () { // 非静默授权，第一次有弹框
      const local = window.location.href
      var code = getUrlParams('code')
      console.log(code)
      if (code === null || code === '' || code === undefined) {
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + process.env.APPID + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
      } else {
        localStorage.removeItem('user')
        this.getOpenId(code) // 把code传给后台获取用户信息
      }
    },
    getOpenId () {
      const { user } = this
      const local = this.funcUrlDel('code')
      // 如果微信授权过来了
      if (getUrlParams('code')) {
        var param = { 'app_id': getUrlParams('app_id'),
          'id': getUrlParams('id'),
          'user_id': getUrlParams('user_id'),
          'source': getUrlParams('source'),
          'code': getUrlParams('code')
        }
        auth(param).then(response => {
          if (response.code === 0) {
            console.log(response)
            var data = response.data
            user.nickname = data.nickname || '' // 当前用户的昵称
            user.headimgurl = data.headimgurl || '' // 当前用户的头像
            user.app_id = data.app_id || ''
            user.id = data.id || 0 // 本人ID
            user.user_id = data.user_id || '' // 种子用户的ID
            user.inviter_id = data.inviter_id || 0 // 邀请人ID
            user.inviter_name = data.inviter_name || 0 // 邀请人姓名
            user.app_name = data.app_name || '看直播' // 页面的title
            user.is_register = data.is_register + '' // 当为普通用户时才有,1为已报名 0为未报名
            user.token = data.token || ''
            user.is_staff = data.is_staff || 0 // 0:普通用户 1:种子用户
            user.storage_id = data.storage_id || 0 // 报名用户ID
            user.source = data.source || '' // 报名用户ID
            localStorage.setItem('user', JSON.stringify(user))
            this.$router.goRight('/home')
          } else {
            alert(response.info)
          }
        }).catch(err => {
          window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + process.env.APPID + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
          console.log(err)
        })
      } else {
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + process.env.APPID + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
        // TODO 弹框 微信授权失败
      }
    },
    funcUrlDel (names) {
      if (typeof (names) === 'string') {
        names = [names]
      }
      var loca = window.location
      var obj = {}
      var arr = loca.search.substr(1).split('&')
      // 获取参数转换为object
      for (var i = 0; i < arr.length; i++) {
        arr[i] = arr[i].split('=')
        obj[arr[i][0]] = arr[i][1]
      }
      // 删除指定参数
      for (var j = 0; j < names.length; j++) {
        delete obj[names[j]]
      }
      // 重新拼接url
      var url = loca.origin + loca.pathname + '?' + JSON.stringify(obj).replace(/["{}]/g, '').replace(/:/g, '=').replace(/,/g, '&')
      return url
    }
  }
}
</script>
